﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;
using Cms.Utilities;

namespace Cms.Web.Framework.DashboardGadgets
{
    public class ScheduledPublishings : BaseGadget
    {
        private Label lblNoHits;
        private Repeater rpScheduledPublishings;
        
        protected override void CreateChildControls()
        {
            try
            {
                base.CreateChildControls();
                rpScheduledPublishings = (Repeater)FindControl("rpScheduledPublishings");
                lblNoHits = (Label)FindControl("lblNoHits");
                rpScheduledPublishings.ItemDataBound += rpScheduledPublishings_ItemDataBound;
                rpScheduledPublishings.DataSource = GetScheduledPublishings();
                rpScheduledPublishings.DataBind();
                lblNoHits.Visible = rpScheduledPublishings.Items.Count == 0;
            }
            catch (Exception ex)
            {
                Logging.LogError("ScheduledPublishings gadget", ex);
                Controls.Clear();
            }
        }

        protected IList<ScheduledPublishing> GetScheduledPublishings()
        {
            return RepositoryFactory.ContentRepository.GetScheduledPublishings(Administrator.CurrentSite.ID);
        }

        protected void rpScheduledPublishings_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            var scheduledPublishing = (ScheduledPublishing)e.Item.DataItem;
            var litVersion = (Literal)e.Item.FindControl("litVersion");
            var litTimestamp = (Literal)e.Item.FindControl("litTimestamp");
            var litRelations = (Literal)e.Item.FindControl("litRelations");
            var lbDelete = (LinkButton)e.Item.FindControl("lbDelete");
            litVersion.Text = "#" + scheduledPublishing.ContentTranslationVersion.ID;
            var timestamp = new DateTime(long.Parse(scheduledPublishing.Timestamp));
            litTimestamp.Text = timestamp.ToShortDateString() + " " + timestamp.ToShortTimeString();
            string relations = string.Empty;
            string link = null;
            foreach (
                ContentSetContainer container in
                    scheduledPublishing.ContentTranslationVersion.Translation.Content.GetContainers())
            {
                if (relations != string.Empty)
                    relations += ", ";
                relations += container.Name;
                if (link != null)
                    continue;
                if (container is ApplicationPage)
                {
                    link = "EditApplicationPage.aspx?tab=content&id=" + container.ID;
                    continue;
                }
                if (container is Page)
                    link = "EditPage.aspx?tab=content&id=" + container.ID;
                if (container is MasterTemplate)
                    link = "EditMasterTemplate.aspx?tab=content&id=" + container.ID;
                if (container is DetailTemplate)
                    link = "EditDetailTemplate.aspx?tab=content&id=" + container.ID;
            }
            litVersion.Text = "<a href=\"Admin/" + link + "\">" + litVersion.Text + "</a>";
            litRelations.Text = relations;
            lbDelete.CommandArgument = scheduledPublishing.ID.ToString();
            lbDelete.Click += lbDeleteScheduledPublishing_Click;
            lbDelete.OnClientClick = "return cms.Dialogs.ConfirmLinkButton(this, " +
                                     "function (status) { " +
                                     "if (status == 'ok') " +
                                     "cms.Lists.ShowPaneOverlay('dashBoardPaneOverlay');" +
                                     "else " +
                                     "cms.Lists.HidePaneOverlays(); " +
                                     "}, " +
                                     "'" + GetText("DeleteHeader") + "', " +
                                     "'" + GetText("DeleteMessage") + "', " +
                                     "'" + GetText("DeleteOK") + "', " +
                                     "'" + GetText("DeleteCancel") + "', " +
                                     "true, " +
                                     "'" + litVersion.Text + "');";
        }

        protected void lbDeleteScheduledPublishing_Click(object sender, EventArgs e)
        {
            IContentRepository contentRepository = new ContentRepository();
            contentRepository.DeleteScheduledPublishing(int.Parse((sender as LinkButton).CommandArgument));
            rpScheduledPublishings.DataSource = GetScheduledPublishings();
            rpScheduledPublishings.DataBind();
            lblNoHits.Visible = rpScheduledPublishings.Items.Count == 0;
        }
    }
}